<?php

namespace App\Http\Controllers;

require_once app_path('Libs') . '/../../vendor/alipay_app/aop/AopClient.php';
require_once app_path('Libs') . '/../../vendor/alipay_app/aop/AopCertification.php';
require_once app_path('Libs') . '/../../vendor/alipay_app/aop/request/AlipayTradeQueryRequest.php';
require_once app_path('Libs') . '/../../vendor/alipay_app/aop/request/AlipayTradeWapPayRequest.php';
require_once app_path('Libs') . '/../../vendor/alipay_app/aop/request/AlipayTradeAppPayRequest.php';

//require_once app_path('Libs') . '/alipay/aop/AopClient.php';
//require_once app_path('Libs') . '/alipay/aop/AopCertification.php';
//require_once app_path('Libs') . '/alipay/aop/request/AlipayTradeQueryRequest.php';
//require_once app_path('Libs') . '/alipay/aop/request/AlipayTradeWapPayRequest.php';
//require_once app_path('Libs') . '/alipay/aop/request/AlipayTradeAppPayRequest.php';


/**
 * 证书类型AopClient功能方法使用测试
 * 1、execute 调用示例
 * 2、sdkExecute 调用示例
 * 3、pageExecute 调用示例
 */
class AliPayAppController extends Controller
{
    public function ali_pay($data, $scene = 'job')
    {
        try {
            $config = config('pay.ali_pay');
            $aop = new \AopClient();

            $aop->gatewayUrl = $config['gatewayUrl'];
            $aop->appId = $config['app_id'];
            $aop->rsaPrivateKey = $config['merchant_private_key'];
            $aop->alipayrsaPublicKey = $config['alipay_public_key'];
            $aop->apiVersion = '1.0';
            $aop->signType = $config['sign_type'];
            $aop->postCharset = $config['charset'];
            $aop->format = 'json';

            $request = new \AlipayTradeAppPayRequest();
            //SDK已经封装掉了公共参数，这里只需要传入业务参数
            $bizcontent = "{\"body\":\"" . $data['body'] . "\","
                . "\"subject\": \"" . $data['subject'] . "\","
                . "\"out_trade_no\": \"" . $data['out_trade_no'] . "\","
                . "\"timeout_express\": \"30m\","
                . "\"total_amount\": \"" . $data['total_amount'] . "\","
                . "\"product_code\":\"QUICK_MSECURITY_PAY\""
                . "}";

            if ($scene == 'job') {
                $notifyUrl = $config['notify_url_job'];
            } elseif ($scene == 'shop') {
                $notifyUrl = $config['notify_url_shop'];
            }
            $request->setNotifyUrl($notifyUrl);
            $request->setBizContent($bizcontent);
            //这里和普通的接口调用不同，使用的是sdkExecute
            $response = $aop->sdkExecute($request);
            //htmlspecialchars是为了输出到页面时防止被浏览器将关键参数html转义，实际打印到日志以及http传输不会有这个问题
//            return htmlspecialchars($response);//就是orderString 可以直接给客户端请求，无需再做处理。
            return $response;
        } catch (Exception $e) {
            //失败返回false
            return false;
        }
    }
}


